enyo.kind({
	name: "webFTP",
	kind: enyo.VFlexBox,
	components: [
		{kind: "DbService", dbKind: "com.palm.webftphosts:1", onFailure: "dbFail", components: [
			{name: "dbGet", method: "find", subscribe: true, onSuccess: "queryResponse", onWatch: "queryWatch"},
			{name: "dbDel", method: "del"},
			{name: "dbPut", method: "put"},
			{name: "dbMerge", method: "merge"}
		]},
		{kind: "MockDb", dbKind: "com.palm.webftphosts:1", onSuccess: "queryResponse", onWatch: "queryWatch"},
		{name: "slidingPane", kind: "SlidingPane", flex: 1, components: [
			{name: "hosts", width: "320px", kind:"SlidingView", components: [
				{kind: "Header", content:"Hosts"},
				{kind: "Scroller", flex: 1, components: [
					{kind: "DbList", name: "hostList", onQuery: "listQuery", onSetupRow: "listSetupRow", components: [
						{kind: "SwipeableItem", tapHighlight: false, confirmCaption: "Delete", onConfirm: "swipeDelete", onclick: "itemClick", components: [
							{kind: "HFlexBox", components: [
								{name: "listItemHostType"},
								{kind: "VFlexBox", flex: 1, components: [
									{name: "listItemTitle", flex: 1, className: "hostList-title"},
									{name: "listItemHost", className: "hostList-host"}
								]},
								{kind: "Button", caption: "?", align: "right", pack: "center"}
							]}
						]}
					]}
				]},
				{kind: "Toolbar", components: [
                    {kind: "GrabButton"}
				]}
			]},
			{name: "main", kind:"SlidingView", flex: 1, components: [
				{kind: "Header", content:"webOS File Transfer"},
				{kind: "VFlexBox", flex: 1, components: [
					{kind: "Scroller", flex: 1, components: [
						{kind: "HFlexBox", flex: 1, components: [
							{kind: "VFlexBox",  flex: 1, components: [
								{kind: "Group", caption: "Local Files", flex: 1, components: [
									{content: "Test", flex: 1}
								]}
							]},
							{kind: "VFlexBox", pack: "center", components: [
  								{kind: "ActivityButton", caption: "->", name: "upload"},
  								{kind: "ActivityButton", caption: "<-", name: "download"}
  							]},
							{kind: "VFlexBox", flex: 1, components: [
	 							{kind: "Group", caption: "Remote Files", flex: 1, components: [
	 						    	{content: "Test", flex: 1}
	 						    ]}
	 						]}
						]}
					]},
					{kind: "DividerDrawer", name: "ftplog", caption: "Log", open: true, components: [
						{flex: 1, kind: "Scroller",  height: "200px", components: [
							{name: "log", style: "color: white; background-color: gray; border: 1px solid black; padding: 4px;", content: "Test Logging", flex: 1}
                        ]}
					]}
				]},
				{kind: "Toolbar", components: [
					{kind: "GrabButton"}
				]}
			]}
		]},
		{kind: "DbInstaller", onSuccess: "installSuccess", onFailure: "dbFail"}
	],
	create: function() {
		this.inherited(arguments);
		this.results = [];
		
		if (window.PalmSystem) {
			this.$.dbInstaller.install(this.$.mockDb.dbKind, enyo.fetchAppId(), this.$.mockDb.data);
		} else {
			console.log("Device required for install Db.");
			this.log("Device required for install Db.");
		}
	},
	log: function(inMessage) {
		this.$.log.setContent(inMessage);
	},
	dbFail: function(inSender, inResponse) {
		this.log("dbService failure: " + enyo.json.stringify(inResponse));
	},
	listQuery: function(inSender, inQuery) {
		// IMPORTANT: must return a request object so dbList can decorate it
		if (window.PalmSystem) {
			inQuery.orderBy = "host";
			return this.$.dbGet.call({query: inQuery});
		} else {
			return this.$.mockDb.call({query: inQuery}, {method: "find"});
		}
	},
	queryResponse: function(inSender, inResponse, inRequest) {
		this.$.hostList.queryResponse(inResponse, inRequest);
	},
	listSetupRow: function(inSender, inRecord, inIndex) {
		//
		// For records marked for deletion: 
		//
		// canGenerate = false prevents a control from rendering any HTML
		this.$.hostList.canGenerate = !inRecord.deleted;
		//
		// we could use showing = false, which renders HTML with display: none, but it's slightly less efficient
		//this.$.hostList.setShowing(!inRecord.deleted);
		//
		// or, we could render 'marked for deletion' rows with a decoration instead of hiding them
		//this.$.hostList.applyStyle("background-color", inRecord.deleted ? "red" : null);
		//
		this.$.hostList.applyStyle("background-color", inRecord.selected ? "#F0F0FF" : null);
		//
//		listItemHostType - For future use to display an icon to denote the type of connection (i.e. FTP or SFTP)
//		listItemTitle - Name given to the connection
//		listItemHost - Actual Host Name
		//this.$.itemIndex.setContent("(" + inIndex + ")");	- Could use to get the index count
		this.$.listItemTitle.setContent(inRecord.title);
		this.$.listItemHost.setContent(inRecord.host);
	},
	reset: function() {
		this.$.hostList.reset();
	},
	queryWatch: function() {
		this.log("dbService watch fired at " + new Date().toLocaleTimeString());
		//
		// NOTE: list.reset() can cause a visible flash on desktop browsers due to asynchrony between 
		// rendering and data acquisition. We're working on a solution.
		//
		this.$.hostList.reset();
	},
	selectedRecordChanged: function(inOldRecord) {
		if (inOldRecord) {
			delete inOldRecord.selected;
		}
		if (this.selectedRecord) {
			this.selectedRecord.selected = true;
		}
		this.$.hostList.refresh();
	},
	itemClick: function(inSender, inEvent) {
		this.setSelectedRecord(this.$.hostList.fetch(inEvent.rowIndex));
	},
	addClick: function() {
		var record = [];
		record.push("GoFlex Home","192.168.2.119",22,"SFTP")
		if (this.selectedRecord) {
			if (window.PalmSystem) {
				this.$.dbPut.call({objects: [record]});
			} else {
				this.$.mockDb.call({objects: [record]}, {method: "put"});
			}
		}
		this.$.hostList.refresh();
	},
	deleteRecord: function(inRecord) {
		if (inRecord) {
			inRecord.deleted = true;
			if (window.PalmSystem) {
				this.$.dbDel.call({ids: [inRecord._id]});
			} else {
				this.$.mockDb.call({ids: [inRecord._id]}, {method: "del"});
			}
		}
	},
	deleteItem: function() {
		this.deleteRecord(this.selectedRecord);
	},
	swipeDelete: function(inSender, inIndex) {
		this.deleteRecord(this.$.hostList.fetch(inIndex));
	},
	//
	// these methods are for installing the mock data into a database on a PalmSystem device
	//
	installDb: function() {
		if (window.PalmSystem) {
			this.$.dbInstaller.install(this.$.mockDb.dbKind, enyo.fetchAppId(), this.$.mockDb.data);
		} else {
			console.log("Device required for install Db.");
		}
	},
	installSuccess: function(inSender) {
		this.$.hostList.punt();
		this.log("install success");
	}
});